package com.app.backend.base.controller;

import com.alibaba.fastjson.JSONObject;
import com.app.backend.base.entity.User;
import com.app.backend.base.service.UserService;
import com.app.backend.base.vo.UserVo;
import com.app.backend.common.annotation.InvokeLog;
import com.app.backend.common.base.BaseController;
import com.app.backend.common.base.BaseService;
import com.app.backend.common.base.PageEntity;
import com.app.backend.common.response.BaseResponse;
import com.app.backend.common.utils.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: UserController
 * @description:
 * @author: Zhun.Xiao
 * @create: 2020-02-08 14:45
 **/


@Slf4j
@RestController
@Api(value = "用户接口")
@RequestMapping(value = "/api/v1/user")
@CacheConfig(cacheNames = "user")
public class UserController extends BaseController<User, String> {

    @Autowired
    private UserService userService;

    @Override
    protected BaseService<User, String> getService() {
        return userService;
    }

    @Override
    @InvokeLog(persistence = true, description = "user get all")
    public BaseResponse<List<User>> getAll() {
        return super.getAll();
    }


    @PostMapping(value = "/demo")
    @InvokeLog(persistence = true, description = "demo")
    public BaseResponse<List<User>> dmemo() {
        User user = new User();
        user.setUsername("wesker");
        user.setUserno("12361");
        user.setSex("male");

        return super.getAll();
    }
    //todo
    //findById


    //todo
    @PostMapping(value = "/getByName")
    @ResponseBody
    @ApiOperation(value = "根据username查找user")
    @InvokeLog(description = "根据username查找user")
    public BaseResponse findByUsername(@RequestBody JSONObject jsonObject) {
        String username = jsonObject.getString("username");
        return new BaseResponse(userService.findByUsername(username));
    }


    @PostMapping(value = "/getAllByCondition")
    @ResponseBody
    @ApiOperation(value = "根据条件获取全部数据")
    @InvokeLog(description = "根据条件获取全部数据")
    public BaseResponse getAllByCondition(@RequestBody UserVo userVo) {
        PageEntity pageEntity = PageEntity.valueOf(userVo);
        return new BaseResponse(userService.findAllByConditionPage(userVo, PageUtil.initPage(pageEntity)));
    }


    public static void main(String[] args) {

        Map<String, String> map = new HashMap<>();

        map.put("key1","value1");
        map.put("key2","value2");
        map.put("key3","value3");


        System.out.println(map);
    }
}